package com.polaris.live.adapter.beans

import android.view.View
import android.widget.LinearLayout
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.TimeUtils
import com.polaris.live.R
import com.polaris.live.common.adapter.BaseBindingAdapter
import com.polaris.live.common.adapter.VBViewHolder
import com.polaris.live.databinding.AdapterWeekRewardsBinding
import com.polaris.live.resp.bean.MyBeansBean

/**
 * MyBeansAdapter
 *
 * @author Created by 半仙 on 2023/10/25/025 9:41
 */
class MyBeansAdapter(private val maxNumber: Long) :
    BaseBindingAdapter<AdapterWeekRewardsBinding, MyBeansBean>() {

    private var mAdapterColor = 0

    override fun convert(holder: VBViewHolder<AdapterWeekRewardsBinding>, item: MyBeansBean) {
        holder.vb.apply {
            var weight = item.num / maxNumber.toFloat() * 10f
            if (weight <= 0.1f || item.num.toFloat() == 0f) {
                weight = 0.1f
            }
            val endWeight = 10 - weight

            val layoutParams = cons.layoutParams as LinearLayout.LayoutParams
            layoutParams.weight = weight
            cons.layoutParams = layoutParams

            val llend = llEnd.layoutParams as LinearLayout.LayoutParams
            llend.weight = endWeight
            llEnd.layoutParams = llend

            weekTime.text = TimeUtils.millis2String(item.data.toLong(), "MM-dd")
            tvNum.text = item.num.toString()
            tvEndNum.text = item.num.toString()

            if (weight >= 2f) {

                llCenter.visibility = View.VISIBLE
                //不知道为什么如果 weight = 10 后一定要隐藏这个 这个宽度才会正常
                if (endWeight == 0f) {
                    llEnd.visibility = View.GONE
                } else {
                    llEnd.visibility = View.INVISIBLE
                }

            } else {
                llEnd.visibility = View.VISIBLE
                llCenter.visibility = View.INVISIBLE
            }

            val index = holder.absoluteAdapterPosition
            if (index + 1 == data.size) {
                weekTime.text = context.getString(com.polaris.live.translation.R.string.today)
            }

            setColor(holder)
        }
    }

    private fun setColor(holder: VBViewHolder<AdapterWeekRewardsBinding>) {
        holder.vb.apply {
            if (mAdapterColor == 0) {
                weekTime.setTextColor(ColorUtils.getColor(R.color.color_924a1c))
                tvEndNum.setTextColor(ColorUtils.getColor(R.color.color_ffaa01))
                weekImg.setImageResource(R.drawable.r12_back_ffaa01)
                if (holder.absoluteAdapterPosition + 1 == data.size) {
                    tvNum.setTextColor(ColorUtils.getColor(R.color.white))
                    weekImg.alpha = 1f
                } else {
                    tvNum.setTextColor(ColorUtils.getColor(R.color.color_ffaa01))
                    weekImg.alpha = 0.3f
                }
            } else {
                weekTime.setTextColor(ColorUtils.getColor(R.color.color_9757ff))
                tvEndNum.setTextColor(ColorUtils.getColor(R.color.color_9757ff))
                weekImg.setImageResource(R.drawable.r12_back_9757ff)
                if (holder.absoluteAdapterPosition + 1 == data.size) {
                    tvNum.setTextColor(ColorUtils.getColor(R.color.white))
                    weekImg.alpha = 1f
                } else {
                    tvNum.setTextColor(ColorUtils.getColor(R.color.color_9757ff))
                    weekImg.alpha = 0.3f
                }
            }
        }
    }

    fun setAdapterColor(position: Int) {
        mAdapterColor = position
        notifyDataSetChanged()
    }
}